<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <saleoutprofitSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true"  show-summary :summary-method="getSummaries"  :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="outOrderNo" slot-scope="{ row }"  >
            <el-link type="primary"   @click="clickPurorder(row)" >{{ row.outOrderNo }}</el-link>
          </template>
        </ele-pro-table>
      </el-card>
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import ExportDialog from '@/components/Excel/ExportDialog'
    import saleoutprofitSearch from './components/saleoutprofit-search.vue';
    import { QuerySaleOutorderProfitSummaryViewByCondition } from '@/api/statistics';
    import { removePageTab } from '@/utils/page-tab-util';

    export default {
      name: 'Saleoutprofit',
      components: {saleoutprofitSearch,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'outOrderNo', label: '出库单号', width: 150, showOverflowTooltip: true,align: 'center',sortable:'outOrderNo',slot:'outOrderNo'},
            {prop: 'customerName', label: '客户名称',width: 150,  showOverflowTooltip: true,align: 'center',sortable:'customerName' },
            {
              prop: 'deliveryDate', label: '发货日期',  width: 150, showOverflowTooltip: true, align: 'center',sortable: 'deliveryDate',
              formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
            },
            {prop: 'currencyDesc', label: '币种', width: 150, showOverflowTooltip: true, align: 'center',sortable:'purAmount'},
            {prop: 'orderAmountCn', label: '本单金额', width: 150, showOverflowTooltip: true, align: 'center',sortable:'orderAmountCn'},
            {prop: 'disAmountCn', label: '优惠金额',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'disAmountCn'},
            {prop: 'amountCn', label: '应收金额',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'amountCn'},
            // {prop: 'preAmountCn', label: '预收款',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'preAmountCn'},
            // {prop: 'recAmountCn', label: '收款金额', width: 150, showOverflowTooltip: true, align: 'center',sortable:'recAmountCn'},
            // {prop: 'recSurplusCn', label: '应收剩余', width: 150, showOverflowTooltip: true, align: 'center',sortable:'recSurplusCn'},
            {prop: 'productCost', label: '出库成本',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'productCost'},
            {prop: 'freight', label: '销售运费', width: 150, showOverflowTooltip: true, align: 'center',sortable:'freight'},
            {prop: 'inAmout', label: '清关费', width: 150, showOverflowTooltip: true, align: 'center',sortable:'inAmout'},
            {prop: 'commissionAmountCn', label: '客户提成',width: 150,  showOverflowTooltip: true, align: 'center',sortable:'commissionAmountCn'},
            {prop: 'profit', label: '毛利', width: 150, showOverflowTooltip: true, align: 'center',sortable:'profit'},
          ],
          //是否详情
          btnIndex: '1',
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,

          orderAmountCnTotal:0,
          disAmountCnTotal:0,
          amountCnTotal:0,
          preAmountCnTotal:0,
          recAmountCnTotal:0,
          recSurplusCnTotal:0,
          productCostTotal:0,
          freightTotal:0,
          inAmoutTotal:0,
          commissionAmountCnTotal:0,
          profitTotal:0,
          // 导出列
          exportList: [
            { name: "出库单号", keys: "outOrderNo", status: true },
            { name: "客户名称", keys: "customerName", status: true },
            { name: "发货日期", keys: "deliveryDate", status: true },
            { name: "币种", keys: "currencyDesc", status: true },
            { name: "本单金额", keys: "orderAmountCn", status: true },
            { name: "优惠金额", keys: "disAmountCn", status: true },
            { name: "应收金额", keys: "amountCn", status: true },
            // { name: "预收款", keys: "preAmountCn", status: true },
            // { name: "收款金额", keys: "recAmountCn", status: true },
            // { name: "应收剩余", keys: "recSurplusCn", status: true },
            { name: "出库成本", keys: "productCost", status: true },
            { name: "销售运费", keys: "freight", status: true },
            { name: "清关费", keys: "inAmout", status: true },
            { name: "客户提成", keys: "commissionAmountCn", status: true },
            { name: "毛利", keys: "profit", status: true },
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }
        
          let data = await QuerySaleOutorderProfitSummaryViewByCondition(params.where);

          this.orderAmountCnTotal = data.orderAmountCnTotal;
          this.disAmountCnTotal= data.disAmountCnTotal;
          this.amountCnTotal= data.amountCnTotal;
          this.preAmountCnTotal= data.preAmountCnTotal;
          this.recAmountCnTotal= data.recAmountCnTotal;
          this.recSurplusCnTotal= data.recSurplusCnTotal;
          this.productCostTotal= data.productCostTotal;
          this.freightTotal= data.freightTotal;
          this.inAmoutTotal= data.inAmoutTotal;
          this.commissionAmountCnTotal= data.commissionAmountCnTotal;
          this.profitTotal= data.profitTotal;


          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          
          return {
              list:data.saleOutorderProfitSummary,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row) {
          this.current = row;
          this.showEdit = true;
        },

        /**
       * @param status 当前行状态
       */
        clickPurorder(row) {
          removePageTab({ key: '/businesshandling/saleoutmanger' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleoutmanger', query: { "orderNo":row.outOrderNo} });
          });
        },

        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: {},
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QuerySaleOutorderProfitSummaryViewByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "销售出库单列表"
              this.$refs.ExportDialog.dataList = data.saleOutorderProfitSummary;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },

        /* 合计 */
        getSummaries(param){
          const sums = [];
          sums[0] = '合计' ;
          const { columns, data } = param;
          columns.forEach((column, index) => {
            if(index ==5){
              sums[index] =  '￥'+ Number(this.orderAmountCnTotal).toFixed(4);
            }else if(index ==6){
              sums[index] =  '￥'+ Number(this.disAmountCnTotal).toFixed(4);
            }else if(index ==7){
              sums[index] =  '￥'+ Number(this.amountCnTotal).toFixed(4);
            }
            // else if(index ==8){
            //   sums[index] =  '￥'+ Number(this.preAmountCnTotal).toFixed(4);
            // }else if(index ==9){
            //   sums[index] = '￥'+ Number(this.recAmountCnTotal).toFixed(4);
            // }else if(index ==10){
            //   sums[index] = '￥'+ Number(this.recSurplusCnTotal).toFixed(4);
            // }
            else if(index ==11){
              sums[index] = '￥'+ Number(this.productCostTotal).toFixed(4);
            }else if(index ==12){
              sums[index] = '￥'+ Number(this.freightTotal).toFixed(4);
            }else if(index ==13){
              sums[index] = '￥'+ Number(this.inAmoutTotal).toFixed(4);
            }else if(index ==14){
              sums[index] = '￥'+ Number(this.commissionAmountCnTotal).toFixed(4);
            }else if(index ==15){
              sums[index] = '￥'+ Number(this.profitTotal).toFixed(4);
            }
          });

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          return sums;
        },

        /* 样式 */
        cellStyle(param){
          const { row, column, rowIndex, columnIndex } = param;
          if(columnIndex==5||columnIndex==6||columnIndex==7||columnIndex==8||columnIndex==9||columnIndex==10||columnIndex==11||columnIndex==12||columnIndex==13||columnIndex==14||columnIndex==15){
            return{
              color:''
            }
          }
        },
      }
    };
  </script>
   <style lang="scss" scoped>
    /*总计字体颜色
    ::v-deep .el-table__footer-wrapper tbody td {
      color: var(--color-primary);
      cursor: pointer;
    }*/
  </style>